package com.example.pattern.draw.progress

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.text.TextPaint
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.example.pattern.R
import com.example.pattern.draw.DrawViewModel
import com.example.pattern.utils.dp2px
import com.example.pattern.utils.sp2px

class ProgressView(context: Context,attrs:AttributeSet?): View(context, attrs)  {
    var mDrawViewModel:DrawViewModel? = null
    private var mMinValue = 0
    private var mMaxValue = 0
    var currentprogress = 0
    private var mDefaultWidth = dp2px(100)
    private var mDefaultHeight = dp2px(10)
    private var mCircleRadius = dp2px(10)
    private var mCornerRadius = mDefaultHeight/2
    private val mBackgroundColor = resources.getColor(R.color.light_dark,null)
    private val mForegroundColor = resources.getColor(R.color.main_red,null)
    private val mTextColor = Color.parseColor("#E5E5E5")
    private val mTextSize = context.sp2px(10).toFloat()
    private val mTextPaint = TextPaint().apply{
       color = mTextColor
       textSize = mTextSize
    }
    private var mProgressPaint = Paint().apply {
        style = Paint.Style.FILL
    }
    init{
        context.obtainStyledAttributes(attrs, R.styleable.ProgressView).apply {
            mMinValue = getInteger(R.styleable.ProgressView_min,1)
            mMaxValue = getInteger(R.styleable.ProgressView_max,50)
            currentprogress = getInteger(R.styleable.ProgressView_current_progress,1)
            recycle()
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var mWidth = MeasureSpec.getSize(widthMeasureSpec)
        if(MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY){
            mWidth = mDefaultWidth
        }
        var mHeight = MeasureSpec.getSize(heightMeasureSpec)
        if(MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY){
            mHeight = mCircleRadius*2
        }
        setMeasuredDimension(mWidth,mHeight)
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        mCornerRadius = height/2
        mCircleRadius = height
        mDefaultHeight = height/2
        mDefaultWidth = width
        mDrawViewModel?.let {viewModel->
            viewModel.setLineWidth(currentprogress)
        }
    }
    //绘制进度条
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        mProgressPaint.color = mBackgroundColor
        canvas.drawRoundRect(0f,(height-mDefaultHeight).toFloat()/2,mDefaultWidth.toFloat(),(height+mDefaultHeight).toFloat()/2,mCornerRadius.toFloat(),mCornerRadius.toFloat(),mProgressPaint)
        mProgressPaint.color = mForegroundColor
        canvas.drawRoundRect(0f,(height-mDefaultHeight).toFloat()/2,currentprogress.toFloat()/mMaxValue*mDefaultWidth,(height+mDefaultHeight).toFloat()/2,mCornerRadius.toFloat(),mCornerRadius.toFloat(),mProgressPaint)
       var cx = currentprogress.toFloat()/mMaxValue*mDefaultWidth
        if(cx<mCircleRadius){
            cx = mCircleRadius.toFloat()
        }
        if(cx+mCircleRadius>width){
            cx = width - mCircleRadius.toFloat()
        }
        //绘制圆点
        canvas.drawCircle(cx,height.toFloat()/2,mCornerRadius.toFloat(),mProgressPaint)
        val text = "$currentprogress"
        val textWidth = mTextPaint.measureText(text)
        val tx = cx-textWidth/2
        var ty = 0f
        mTextPaint.fontMetrics.apply {
            ty = (height - this.top - this.bottom)/2
        }
        canvas.drawText(text,tx,ty,mTextPaint)
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when(event?.action) {
            MotionEvent.ACTION_DOWN -> {changeProgress(event.x)}
            MotionEvent.ACTION_MOVE -> {changeProgress(event.x)}
            MotionEvent.ACTION_UP -> {
                mDrawViewModel?.let{viewModel->
                    viewModel.setLineWidth(currentprogress)
                }
            }
        }
        return true
    }
    private fun changeProgress(tx:Float){
        if(tx <=0|| tx>width) return
        currentprogress = (tx/width*mMaxValue).toInt()
        invalidate()
    }

}